<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Hash;
use DB;
class A_userController extends Controller
{
   public function getAdd(){
        return view('admin/a_user/add');
   }
   public function postInsert(Request $request){
        $this->validate($request, [
            'a_uname'=>'required',
            'a_upassword'=>'required',
            'repassword'=>'required|same:a_upassword'
        ],[
            'username.required'=>'用户名必填',
            'a_upassword.required'=>'密码必填',
            'repassword.required'=>'重置密码必填',
            'repassword.same'=>'重置密码要和密码一致'
        ]);
        $data = DB::table('admin_users') -> where('a_uname',$request -> input('a_uname')) -> first();
        if(!empty($data)){
            return back() -> withInput() -> with('error','用户名已存在');
        }
        // 判断两次密码是否一致
        $arr = $request -> only('a_uname','a_uauth');
        $a = $request -> input();
        //判断是否有文件上传
        if($request -> hasFile('a_upic')){
            //随机文件名称
            $name = rand(111111,999999).time();
            //获取文件后缀名
            $a = $request -> file('a_upic') ->getClientOriginalExtension();
            //拼接文件名称
            $pic_name = $name.'.'.$a;
            $arr['a_upic'] = $pic_name;
            //移动文件到指定位置
            $request -> file('a_upic') -> move('./public/images/',$pic_name);
        }
        $arr['a_upassword'] = Hash::make($request -> input('a_upassword'));

        $res = DB::table('admin_users') -> insert($arr);
        if($res){
            return redirect('/admin/a_user/index') -> with('success','添加成功');
        }else{
            return back() -> withInput() -> with('error','添加失败');
        }
   }
   public function getIndex(){
        $data = DB::table('admin_users') -> get();
        return view('/admin/a_user/index',['data'=>$data]);
   }
   public function getDelete($id){
        $res = DB::table('admin_users') -> where('a_uid',$id) -> delete();
        if($res){
            return redirect('/admin/a_user/index') -> with('success','删除成功');
        }else{
            return back() -> withInput() -> with('error','删除失败');
        }
   }
   public function getEdit($id){
       $data =  DB::table('admin_users') -> where('a_uid',$id) -> first();
        return view('admin/a_user/edit',['data'=>$data]);
   }
   public function postUpdate($id,Request $request){
        $arr = $request -> only('a_uname');
        if($request -> hasFile('pic')){
            $temp_name = rand(111111,999999).time();
            $hz = $request -> file('pic') -> getClientOriginalExtension();
            $name = $temp_name.'.'.$hz;
            $arr['a_upic'] = $name;
            $request -> file('pic') -> move('./public/images/',$name);
        }
        // $data = DB::table('admin_users') -> where('a_uid',$id) -> first();
        // $arr['a_upic'] = $request -> input('pic',$data['a_upic']);
        // dd($arr);
        $res = DB::table('admin_users') -> where('a_uid',$id) -> update($arr);
        if($res){
            return redirect('/admin/a_user/index') -> with('success','修改成功');
        }else{
            return back() -> withInput() -> with('error','修改失败');
        }
   }
}
